package day8;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class StringTest {

    private boolean queryString;

    @Test
    public void testInit(){
        String s1=new String("abc");

        //早期多行字符串只能通过+进行连接
        String s2="第一行内容..."+
                "第二行内容...";

        //"""表示多行字符串
        String s3= """
                第一行内容...
                第二行内容...
                """;
    }

    @Test
    public void test1(){
        String url="www.hunau.edu.cn/xky/index.html?query=计科&keyword=web";
       //获取第一个/的位置
        int index1=url.indexOf('/');
        String host=url.substring(0,index1);
        System.out.println(host);

        int indexQuery=url.indexOf('?');
        String query=url.substring(indexQuery+1);
        System.out.println(queryString);
        //根据&对字符串进行切分
        String[] kvs=query.split("&");
        System.out.println(Arrays.toString(kvs));
    }

    @Test
    public void testEncoder() throws Exception{
        String s1="a科计";
        //[97, -24, -82, -95, -25, -89, -111]
        byte[] utf8Bytes=s1.getBytes("utf-8");
        System.out.println(Arrays.toString(utf8Bytes));
        //[97, -68, -58, -65, -58]
        byte[] gbBytes=s1.getBytes("GBK");
        System.out.println(Arrays.toString(gbBytes));
        //采用当前源文件中默认的编码进行处理
        byte[] defaultBytes=s1.getBytes();
        System.out.println(Arrays.toString(defaultBytes));

    }

    @Test
    public void testDecoder() throws Exception{
        byte[] utf8Bytes=new byte[]{97, -24, -82, -95, -25, -89, -111};
        //指定utf-8进行解码
        String s1=new String(utf8Bytes,"utf-8");
        System.out.println(s1);
        ////如果编码和解码的字符集不一致,则会出现乱码
        String s2=new String(utf8Bytes,"GBK");
        System.out.println(s2);
    }

    @Test
    public void textEx() throws UnsupportedOperationException{
        String s1="抗日胜利80周年";
        try {
            byte[] bytes= s1.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("[额外功能]:记录日志 UnsupportedEncodingException");
        }catch(Exception e){
        System.out.println("测试语句...");
    }
}
}
